/**
 *
 * @authors wmn1525 (wmn1525@gmail.com)
 * @date    2020/08/21
 * @description 定义路由拦截器模块，比如登录鉴权逻辑等
 */
import router from './index';
import { publicAPIModule } from "@/store/modules/public";
import { GetSocketModule } from "@/store/modules/GetSocketData";
import { JointInspectionModule } from "@/store/modules/JointInspection";
import lodash from 'lodash'
import { Route } from 'vue-router';
import { MessageBox } from 'element-ui'
import pointType from '@/utils/pointType';
import fetch from '../servers/index'
import { AppModule } from "@/store/modules/app";

var getToken = function (): boolean {
  var storage = window.localStorage;
  var userinfs: string | null = storage.getItem("userinf");
  // var jsonObj = JSON.parse(userinfs || "");
  if (userinfs) {
    return true
  } else {
    return false
  }

}
// console.log(getToken());

const whiteList = ['/login']; // no redirect whitelist
let getAllAuxiliaryPointer = (cb) => {
  // publicAPIModule.setpointType()
  if (!GetSocketModule.AllAuxiliaryPointer.length) {
    publicAPIModule.setPointer().then(() => {
      // console.log("44444444444444:", publicAPIModule.pointType);
      GetSocketModule.getAllAuxiliaryPointer()
        .then((data) => {
          // console.log(12121, data)
          // 判断台账有没有数据
          // if (data.length) {
          //   cb()
          // } else {
          //   cb('/login')
          // }
          // 暂时
          cb()
        })
        .catch((err) => { 
          // console.log("2222222222222222222:", err)
          MessageBox.confirm('访问台账数据错误，是否重新访问！', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消'
          })
            .then(() => {
              getAllAuxiliaryPointer(cb)
            })
            .catch(() => { cb() })


        })
    })
      .catch((err) => {
        console.log(err);
        MessageBox.confirm('获取pointType字典错误，是否重新访问！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
          .then(() => {
            getAllAuxiliaryPointer(cb)
          })
          .catch(() => { })
        // cb()
      });

  } else {
    cb()
  }
}
// 获取公共字典
let fetchPublic = () => {
  if (lodash.isEmpty(publicAPIModule.stationTreeList)) {
    publicAPIModule.setstationTreeList({}); //公共方法-变电站树图
  }
  if (lodash.isEmpty(publicAPIModule.getAreaDictList)) {
    publicAPIModule.getAreaDict(); //获取区域字典
  }
  if (lodash.isEmpty(publicAPIModule.stationList)) {
    publicAPIModule.queryStations(); //下拉框变电站
  }
  if (lodash.isEmpty(publicAPIModule.queryAlermStatusList)) {
    publicAPIModule.queryAlermStatus(); //下拉框报警状态
  }
  if (lodash.isEmpty(publicAPIModule.queryCitysList)) {
    publicAPIModule.queryCitys(); //下拉框所属地市
  }
  if (lodash.isEmpty(publicAPIModule.querySystemsList)) {
    publicAPIModule.querySystems(); //下拉框所属系统
  }
  if (lodash.isEmpty(publicAPIModule.AlarmSourceList)) {
    publicAPIModule.getAlarmSource(); //下拉框告警来源
  }

  // FanYu
  if (lodash.isEmpty(publicAPIModule.CompanyList)) {
    publicAPIModule.setCompanyList();         //公共接口-下拉框单位名称
  }
  if (lodash.isEmpty(publicAPIModule.VoltageList)) {
    publicAPIModule.setVoltageList();         //公共接口-下拉框变电站电压等级
  }
  if (lodash.isEmpty(publicAPIModule.SubstationList)) {
    publicAPIModule.setSubstationList();      //公共接口-下拉框变电站
  }
  if (lodash.isEmpty(publicAPIModule.TeamList)) {
    publicAPIModule.setTeamList();            //公共接口-下拉框运维班组
  }
  if (lodash.isEmpty(publicAPIModule.DeviceTypeList)) {
    publicAPIModule.setDeviceTypeList();      //公共接口-下拉框设备类型
  }

  if (lodash.isEmpty(JointInspectionModule.CompanyList)) {
    JointInspectionModule.setCompanyList();     //公共方法-当前用户所属地市
  }

  // if (lodash.isEmpty(publicAPIModule.queryHasDataStationsList)) {
  //   publicAPIModule.queryHasDataStations();     //公共方法-下拉框有数据变电站
  // }


}

router.beforeEach(async (to: Route, from: Route, next: any) => {
  console.log(12345,to,from)

  if (getToken()) {
    if (to.path === '/login') {
      var storage = window.localStorage;
      storage.setItem("userinf", "");
      next();
    } else {
      // 权限
      fetchPublic()
      getAllAuxiliaryPointer(next)
    }
  } else {
    /* has no token*/
    if (to.path === '/login') {
      next()
    } else if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单，直接进入
      fetchPublic()
      getAllAuxiliaryPointer(next)
    } else {
      next(`/login`); // 否则全部重定向到登录页
    }
  }

  
});

